﻿--[[
   @brief    game download mgr
   @author   maxyao
   @version  1.0
   @date     2019-04-25
--]]

download_mgr_view = 
{
    pDialog_ = nil;
    dwRootId_ = 0;
    pWeb_ptr_ = nil;
    pClose_btn_dlg = nil;
    -- shadow_ptr_ = nil;
    shadow_id_ = 0;

    page_url = LoadStr("GAME_DOWNLOAD_MGR_URL");
}

function download_mgr_view:init()
    base_utility.log("[download_mgr_view] init ");
end

function download_mgr_view:show(bShow)
    if bShow == true then
        if self.pDialog_ ~= nil then
            self.pDialog_:Restore();
            self:display_wnd();
        else
            self:create();
        end;
    else
        if self.pDialog_ ~= nil then
            self.pDialog_:Show(FALSE);
        end
    end
end

function download_mgr_view:create()
    if self.pDialog_ ~= nil then
        return true;
    end
    -- 先创建阴影，防止收不到on_show的通知
    -- self.shadow_id_ = layout_mgr.restore_layout_multi("download_mgr\\dlg_shadow.tpf");
    -- self.shadow_ptr_ = ui_utility.get_ctrl_ptr_by_id(self.shadow_id_);
--    ui_utility.setproperty_by_id(self.shadow_id_, "Width", "714");
--    ui_utility.setproperty_by_id(self.shadow_id_, "Height", "599");

    self.dwRootId_ = layout_mgr.restore_layout("download_mgr\\game_download_mgr.tpf");
    if self.dwRootId_ ~= 0 then
        self.pDialog_ =ui_utility.get_ctrl_ptr_by_id(self.dwRootId_);
        self.pWeb_ptr_ = ui_utility.get_ctrl_ptr_by_path(self.dwRootId_, "QBWebBrowser_DL_MGR");
        self.pClose_btn_dlg = ui_utility.get_ctrl_ptr_by_path(self.dwRootId_, "CloseBtnCtn");
        if self.pDialog_ ~= nil then
            --self.pClose_btn_dlg:SetParent(self.pDialog_);
            self.pClose_btn_dlg:MoveFollow(self.pDialog_:GetControlID(), 409, 9);
            self.pDialog_:MoveToCenter();
        end
        if self.pWeb_ptr_ ~= nil then 
            self:flush_web();
            if self.pWeb_ptr_:IsVisible() == FALSE then
                local web_id = self.pWeb_ptr_:GetControlID();
                ui_utility.setproperty_by_id(
                    web_id, "OnNavigateFailed", "download_mgr_view:on_navigate_fail");
                --页面加载阻塞导致这个接口延迟，改为直接由页面控制加载
                ui_utility.setproperty_by_id(
                    web_id, "OnNavigateComplete", "download_mgr_view:on_navigate_ok");
            end
        end
        return true;
    else
        base_utility.log("[download_mgr_view] download_mgr_view error!!!");
        return false;
    end
end

function download_mgr_view:hide(uilib, ctrl_id)
    base_utility.log("[download_mgr_view] hide download_mgr_view tpf");
    if self.pDialog_ ~= nil then
        self.pDialog_:Show(FALSE);
    end
end

function download_mgr_view:flush_web()
    if self.pWeb_ptr_ ~= nil then
        base_utility.mainframe_report("download_mgr", "browser", "start_navigate");
        local ui_str, str_holder = U8toCUIStr(self.page_url);
        self.pWeb_ptr_:Navigate(ui_str);
    end
end

function download_mgr_view:on_navigate_ok()
    base_utility.log("[download_mgr_view]Web navigate success");
    --[[local loading_ctrl = ui_utility.get_ctrl_ptr_by_cid(CID_DL_MGR_DLG_LOADING);
    if loading_ctrl ~= nil then
        loading_ctrl:Show(FALSE);
    end--]]
    base_utility.mainframe_report("download_mgr", "browser", "navigate_ok");
    self:display_wnd();
end

function download_mgr_view:display_wnd()
    if ui_utility.get_dpi_scale() == 1.0 then
        --shadow_helper.create_shadow(self.dwRootId_);
    end;
    self.pDialog_:Show(TRUE);
    self.pDialog_:BringToTop();
end

function download_mgr_view:on_navigate_fail()
    base_utility.log("[download_mgr_view]Web navigate fail");
    base_utility.mainframe_report("download_mgr", "browser", "navigate_failed");
end

function download_mgr_view:close()
    base_utility.log("[download_mgr_view] remove download_mgr_view tpf");
    if self.pDialog_ ~= nil then
        --shadow_helper.destroy_shadow(self.dwRootId_);
        self.pDialog_:Show(FALSE);
    end
    layout_mgr.delay_remove_ctrl(self.dwRootId_);
    self.dwRootId_ = 0;
    self.pDialog_ = nil;   
    if self.shadow_ptr_ ~= nil then
        self.shadow_ptr_:Show(FALSE);
    end
    layout_mgr.delay_remove_ctrl(self.shadow_id_);
    self.shadow_id_ = 0;
    self.shadow_ptr_ = nil; 
end

function download_mgr_view:min()
    if self.pDialog_ ~= nil then
        self.pDialog_:Minimize();
    end
end

function download_mgr_view:has_creat()
    return self.dwRootId_ ~= 0;
end


function download_mgr_view:resize(width, height)
    base_utility.log("[download_mgr_view] resize");
  
    if width > 0 and height > 0 then
        if nil ~= self.pDialog_ then
            local rtTemp = self.pDialog_:GetRectEx(FALSE,TRUE);

            rtTemp.right  = rtTemp.left + width + 2;
            rtTemp.bottom = rtTemp.top  + height + 2;

            self.pDialog_:MoveEx(rtTemp,TRUE);
        end;
    end
end

function download_mgr_view:on_show(uilib, ctrl_id)
    if self.pWeb_ptr_ ~= nil and self.pDialog_:IsDisable() == FALSE then
        self.pWeb_ptr_:SetFocus();
    end
--    if nil ~= self.shadow_ptr_ then
--        self.shadow_ptr_:Show(TRUE);
--    end
end

--function download_mgr_view:on_wnd_pos_changed()
--    if nil ~= self.shadow_ptr_ and nil ~= self.pDialog_ then
--        local     rtThis = self.pDialog_:GetRectEx(TRUE,TRUE);
--        local     rtTemp = self.shadow_ptr_:GetRectEx(TRUE,TRUE);
--        local     nWidth = rtThis.right - rtThis.left + 32;
--        local     nHeight= rtThis.bottom - rtThis.top + 32;

--        rtTemp.left  = rtThis.left - 16;
--        rtTemp.top   = rtThis.top  - 6;
--        rtTemp.right = rtTemp.left + nWidth;
--        rtTemp.bottom= rtTemp.top  + nHeight;

--        self.shadow_ptr_:MoveEx(rtTemp,TRUE);
--    end;
--end

function download_mgr_view:on_hide()
--    if nil ~= self.shadow_ptr_ then
--        self.shadow_ptr_:Show(FALSE);
--    end
end

function download_mgr_view:set_focus_dialog()
    if self.pDialog_ ~= nil then
        self.pDialog_:SetFocus();
    end
end

function download_mgr_view:show_close_btn(show)
    if show ~= nil and show == true then
        if self.pClose_btn_dlg ~= nil then
            self.pClose_btn_dlg:Show(TRUE);
        end;
    else
        if self.pClose_btn_dlg ~= nil then
            self.pClose_btn_dlg:MoveFollow(0, 0, 0);
            self.pClose_btn_dlg:Show(FALSE);
        end;
    end; 
end;

download_mgr_view.init();

